﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hrm.Core;

namespace Hrm.Services
{
    public class LogService : BaseService<Log>, ILogService
    {
        ILogNoteDbRepository _logNoteDbRepository;

        public LogService(ILogNoteDbRepository logNoteDbRepository)
            : base(logNoteDbRepository)
        {
            _logNoteDbRepository = logNoteDbRepository;
        }

        public bool Trace(string message)
        {
            return Log(message, (int)EnumLogLevel.Trace);
        }

        public bool Trace(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Trace);
        }

        public bool Trace(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Trace);
        }

        public bool Debug(string message)
        {
            return Log(message, (int)EnumLogLevel.Debug);
        }

        public bool Debug(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Debug);
        }

        public bool Debug(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Debug);
        }

        public bool Info(string message)
        {
            return Log(message, (int)EnumLogLevel.Info);
        }

        public bool Info(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Info);
        }

        public bool Info(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Info);
        }

        public bool Warn(string message)
        {
            return Log(message, (int)EnumLogLevel.Warn);
        }

        public bool Warn(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Warn);
        }

        public bool Warn(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Warn);
        }

        public bool Error(string message)
        {
            return Log(message, (int)EnumLogLevel.Error);
        }

        public bool Error(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Error);
        }

        public bool Error(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Error);
        }

        public bool Fatal(string message)
        {
            return Log(message, (int)EnumLogLevel.Fatal);
        }

        public bool Fatal(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Fatal);
        }

        public bool Fatal(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Fatal);
        }

        public virtual bool Log(string meesage, int logLevel)
        {
            Log lm = new Log()
            {
                CreateDatetime=DateTime.Now,
                Level=logLevel,
                Message=meesage,
                StackTrade = string.Empty,
                Origin = string.Empty
            };
            return _logNoteDbRepository.Insert(lm)>0;
        }

        public virtual bool Log(Exception ex, int logLevel)
        {
            Log lm = new Log()
            {
                CreateDatetime = DateTime.Now,
                Level = logLevel,
                Message = ex.Message,
                StackTrade=ex.StackTrace,
                Origin=ex.Source
            };
            return _logNoteDbRepository.Insert(lm)>0;
        }

        public virtual bool Log(string meesage, Exception ex, int logLevel)
        {
            Log lm = new Log()
            {
                CreateDatetime = DateTime.Now,
                Level = logLevel,
                Message = meesage+"\t\nException:"+ex.Message,
                StackTrade = ex.StackTrace,
                Origin = ex.Source
            };
            return _logNoteDbRepository.Insert(lm)>0;
        }

        public virtual bool Log(Log lm)
        {
            return _logNoteDbRepository.Insert(lm) > 0;
        }
    }
}
